package com.itheima.interceptor;

import com.itheima.utils.JwtUtils;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
@Component
@Slf4j
public class TokenInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        String url = request.getRequestURL().toString();
        if(url.contains("login")){
            log.info("login接口放行");
            return true;
        }
        String token = request.getHeader("token");
        if(token==null||token.length() == 0){
            log.info("token为null或为空");
            response.setStatus(401);
            return false;
        }
        try {
            JwtUtils.parseJWT(token);
            return true;
        }catch (Exception e){
            response.setStatus(401);
            return false;
        }
    }
}
